package com.twu.biblioteca;

import com.twu.exception.ShelfException;
import com.twu.exception.MenuException;
import java.util.HashMap;
import java.util.Map;

public class Library {

    private BookShelf bookShelf;
    private HashMap<Book, Customer> lentBooks = new HashMap<Book, Customer>();
    private MovieShelf movieShelf;
    private HashMap<Movie, Customer> lentMovies = new HashMap<Movie, Customer>();

    public Library() throws MenuException, ShelfException {
        setDefaultBooks();
        setDefaultMovies();
    }


    private void setDefaultBooks() throws ShelfException {
        bookShelf = new BookShelf(lentBooks);
        bookShelf.add(new Book(1, "TDD by example", "Kent Beck"));
        bookShelf.add(new Book(2, "Head first java", "Cathy Sierra"));
    }

    public String checkOutBook(Customer customer, String title, String author) throws ShelfException {
        Book book = bookShelf.fetch(title, author);
        lentBooks.put(book, customer);
        return LibraryConstants.SUCCESSFUL_BOOK_CHECKOUT_MSG + book.number();
    }

    public String returnBook(Book book, Customer customer) {
        for (Map.Entry<Book, Customer> entry : lentBooks.entrySet()) {
            if (entry.getKey().equals(book) && entry.getValue().equals(customer)) {
                lentBooks.remove(entry.getKey());
                return LibraryConstants.SUCCESSFUL_BOOK_RETURN_MSG;
            }
        }
        return LibraryConstants.UNSUCCESSFUL_BOOK_RETURN_MSG;
    }

    public String books() {
        return LibraryConstants.AVAILABLE_BOOKS + bookShelf.books();
    }

    private void setDefaultMovies() throws ShelfException {
        movieShelf = new MovieShelf(lentMovies);
        Movie invincible = new Movie(1, "Invicible", 2014, "Jafari", Rating.Eight);
        movieShelf.add(invincible);
        Movie incredible = new Movie(2, "Incredible", 2012, "John", Rating.Six);
        movieShelf.add(incredible);
    }

    public String checkOutMovie(Customer customer, String title) throws ShelfException {
        Movie movie = movieShelf.fetch(title);
        lentMovies.put(movie, customer);
        return LibraryConstants.SUCCESSFUL_MOVIE_CHECKOUT_MSG + movie.number();
    }

    public String returnMovie(Movie movie, Customer customer) {
        for (Map.Entry<Movie, Customer> entry : lentMovies.entrySet()) {
            if (entry.getKey().equals(movie) && entry.getValue().equals(customer)) {
                lentMovies.remove(entry.getKey());
                return LibraryConstants.SUCCESSFUL_MOVIE_RETURN_MSG;
            }
        }
        return LibraryConstants.UNSUCCESSFUL_MOVIE_RETURN_MSG;
    }

    public String movies(){
        return LibraryConstants.AVAILABLE_MOVIES + movieShelf.movies();
    }

    public Movie returningMovie(int number) throws ShelfException {
        return movieShelf.returningMovie(number);
    }

    public Book returningBook(int number) throws ShelfException {
        return bookShelf.returningBook(number);
    }
}
